UNPKG

@pexip/signal

Version:

an observer pattern while avoiding boilerplate code. https://en.wikipedia.org/wiki/Signals_and_slots

168 lines (105 loc) 3.15 kB
Signal Interface returned from `createSignal`. ## Type Parameters | Type Parameter | Default type | | ------ | ------ | | `Subject` | - | | `Observable` | `Subject` | ## Properties | Property | Type | Description | | ------ | ------ | ------ | | <a id="name"></a> `name?` | `string` | - | | <a id="size"></a> `size` | `number` | **`Internal`** A function to get the current size of observers for internal testing purpose. | ## Methods ### add() ```ts add(observer, options?): Detach; ``` Add a new observer to receive the signal #### Parameters | Parameter | Type | Description | | ------ | ------ | ------ | | `observer` | [`Observer`](../type-aliases/Observer.mdx)\<`Observable`\> | A function to be called when there is a new signal | | `options?` | \{ `context?`: `unknown`; `signal?`: `AbortSignal`; \} | - | | `options.context?` | `unknown` | - | | `options.signal?` | `AbortSignal` | - | #### Returns [`Detach`](../type-aliases/Detach.mdx) function to detach the observer from the signal see DetachFn #### Remarks If the `context` is not provided it assumes the `observer` has been [\`bind\`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) before it is passed to the signal. *** ### addOnce() ```ts addOnce(observer, options?): Detach; ``` Add a new observer and got automatically detached immediately after the first signal. #### Parameters | Parameter | Type | Description | | ------ | ------ | ------ | | `observer` | [`Observer`](../type-aliases/Observer.mdx)\<`Observable`\> | A function to be called when there is a new signal | | `options?` | \{ `context?`: `unknown`; `signal?`: `AbortSignal`; \} | - | | `options.context?` | `unknown` | - | | `options.signal?` | `AbortSignal` | - | #### Returns [`Detach`](../type-aliases/Detach.mdx) function to detach the observer from the signal see DetachFn #### Remarks If the `context` is not provided it assumes the `observer` has been [\`bind\`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) before it is passed to the signal. *** ### remove() ```ts remove(observer): void; ``` **`Internal`** Remove the `observer` from the signal construct. Please use the returned function from [Signal.add](#add) instead #### Parameters | Parameter | Type | Description | | ------ | ------ | ------ | | `observer` | [`Observer`](../type-aliases/Observer.mdx)\<`Observable`\> | the `observer` was passed in earlier | #### Returns `void` *** ### emit() #### Call Signature ```ts emit(): void; ``` Emit signal. ##### Returns `void` ##### Remarks It will print a warning when it is trying to emit a signal when there is no observer. #### Call Signature ```ts emit(subject): void; ``` ##### Parameters | Parameter | Type | | ------ | ------ | | `subject` | `Subject` | ##### Returns `void` #### Call Signature ```ts emit(subject?): void; ``` ##### Parameters | Parameter | Type | | ------ | ------ | | `subject?` | `Subject` | ##### Returns `void` *** ### clearBuffers() ```ts clearBuffers(): void; ``` Clear the buffers (if any) #### Returns `void`