-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: promise store, flush pending request when state is deleted - …
…fix #73
- Loading branch information
Showing
6 changed files
with
109 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { idGenerator } from '@ircam/sc-utils'; | ||
|
||
const generateRequestId = idGenerator(); | ||
|
||
export default class PromiseStore { | ||
constructor(name) { | ||
this.name = name; | ||
this.store = new Map(); | ||
} | ||
|
||
add(resolve, reject, type) { | ||
const reqId = generateRequestId.next().value; | ||
this.store.set(reqId, { resolve, reject, type }); | ||
|
||
return reqId; | ||
} | ||
|
||
resolve(reqId, data) { | ||
const { resolve } = this.store.get(reqId); | ||
this.store.delete(reqId); | ||
|
||
resolve(data); | ||
} | ||
|
||
reject(reqId, msg) { | ||
const { reject } = this.store.get(reqId); | ||
this.store.delete(reqId); | ||
|
||
reject(new Error(msg)); | ||
} | ||
|
||
// reject all pendeing request | ||
flush() { | ||
for (let [_reqId, entry] of this.store) { | ||
const { reject, type } = entry; | ||
reject(new Error(`[${this.name}] Discard promise "${type}", cannot resolve`)); | ||
} | ||
|
||
this.store.clear(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.