-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from lroskoshin/migrate-to-new-injection
Migrate-to-new-injection
- Loading branch information
Showing
23 changed files
with
9,271 additions
and
10,148 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
{ | ||
"parser": "@typescript-eslint/parser", | ||
"plugins": ["@typescript-eslint", "jest"], | ||
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"], | ||
"extends": ["eslint:recommended", "plugin:@typescript-eslint/eslint-recommended", "plugin:@typescript-eslint/recommended"], | ||
"rules": { | ||
"eol-last": ["error", "always"], | ||
"no-multiple-empty-lines": "error", | ||
"semi": ["error", "always"], | ||
"quotes": ["error", "single"] | ||
"quotes": ["error", "single"], | ||
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }], | ||
"no-console": "error" | ||
} | ||
} |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { Subscribable, Unsubscriber } from './insertion'; | ||
|
||
export function insertAttr(socket: Element, attrName: string, insertion: string | Subscribable<string>): Unsubscriber { | ||
export function insertAttr(socket: Element, attrName: string, insertion: string | Subscribable<string>): Unsubscriber | void { | ||
const attr = document.createAttribute(attrName); | ||
socket.setAttributeNode(attr); | ||
if(typeof insertion === 'string') { | ||
attr.value = insertion; | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
return () => {}; | ||
} else { | ||
const subscription = insertion.subscribe((value: string) => { | ||
const unsubscriber = insertion((value: string) => { | ||
attr.value = value; | ||
}); | ||
return subscription.unsubscribe; | ||
return unsubscriber; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,43 +1,44 @@ | ||
export type Unsubscriber = () => void; | ||
|
||
export type Unsubscribable = { | ||
unsubscribe: Unsubscriber; | ||
} | ||
|
||
//TO-DO: remove unsubscriber after onDestroy hook will be added | ||
export type Subscribable<T> = { | ||
subscribe(listner: (value: T) => void): Unsubscribable; | ||
(listner: (value: T) => void): Unsubscriber | void; | ||
}; | ||
|
||
export type Subscription<T> = { | ||
next(value: T): void; | ||
(value: T): void; | ||
}; | ||
|
||
type Subscriptions<T> = T extends infer K ? Subscription<K> : never; | ||
|
||
export interface Component { | ||
unsubscribe(): void; | ||
fragment: DocumentFragment; | ||
} | ||
export type StaticInsertion = string | Component; | ||
export type DynamicInsertion = Subscribable<string | Component>; | ||
export type ValueInsertion = DynamicInsertion | StaticInsertion; | ||
export type Instertion = Subscription<Event> | ValueInsertion; | ||
export type Instertion = Subscriptions<GlobalEventHandlersEventMap[keyof GlobalEventHandlersEventMap]> | ValueInsertion; | ||
// TO-DO: Optimize type guarding | ||
export function isStaticInsertion(insertion: Instertion): insertion is StaticInsertion { | ||
return typeof insertion === 'string' || 'fragment' in insertion; | ||
} | ||
export function isDynamicInsertion(insertion: Instertion): insertion is StaticInsertion { | ||
return typeof insertion !== 'string' && 'subscribe' in insertion && typeof insertion.subscribe === 'function'; | ||
|
||
export function isDynamicInsertion(insertion: Instertion): insertion is DynamicInsertion { | ||
return typeof insertion === 'function'; | ||
} | ||
|
||
export function ensureValueInsertion(instertion: Instertion): ValueInsertion { | ||
if(isStaticInsertion(instertion) || isDynamicInsertion(instertion)) { | ||
return instertion; | ||
} | ||
} | ||
|
||
throw new Error('The passed value is not Instertable.'); | ||
} | ||
|
||
export function ensureSubscription(instertion: Instertion): Subscription<unknown> { | ||
if(typeof instertion !== 'string' && 'next' in instertion && typeof instertion.next === 'function') { | ||
return instertion; | ||
} | ||
export function ensureSubscription(instertion: Instertion): Subscription<Event> { | ||
if(typeof instertion === 'function') { | ||
return instertion as Subscription<Event> ; | ||
} | ||
|
||
throw new Error('The passed value is not a Subscription.'); | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { mergeComponents } from './merge-components'; | ||
export { state } from './state'; |
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,22 @@ | ||
type Dispatcher<Value> = { | ||
(value: Value): void | ||
} | ||
|
||
type State<Value> = { | ||
(cb: (value: Value) => void): void; | ||
} | ||
|
||
export function state<Value>(value: Value): [State<Value>, Dispatcher<Value>] { | ||
let currentValue = value; | ||
const linsteners: Array<(value: Value) => void> = []; | ||
return [ | ||
(cb) => { | ||
cb(currentValue); | ||
linsteners.push(cb); | ||
}, | ||
(newValue: Value) => { | ||
currentValue = newValue; | ||
linsteners.forEach((cb) => cb(currentValue)); | ||
} | ||
]; | ||
} |
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
Oops, something went wrong.