Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript-ify #92

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ packages/*/visual.html
/packages/tko/dist/tko.es6.min.js
/packages/tko/dist/tko.es6.min.js.map
/.vscode
docs
2 changes: 2 additions & 0 deletions Backers.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Our sincerest thanks for the financial support.

☕️ ☕️ Colleen Hood


☕️ Pietro Vertechi
☕️ Andrew Vickers
☕️ Jonathan Carter
☕️ Michael Balsom
Expand Down
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"url": "https://github.com/knockout/tko.git"
},
"scripts": {
"prepublish": "yarn build",
"prepublishOnly": "yarn build",
"test": "lerna exec --concurrency=1 --loglevel=warn -- yarn test",
"fast-test": "lerna exec --concurrency=6 --loglevel=warn -- yarn test",
"build": "lerna exec --concurrency=6 --loglevel=warn -- yarn build",
Expand Down Expand Up @@ -38,6 +38,7 @@
]
},
"devDependencies": {
"@types/jquery": "^3.3.29",
"chai": "^4.1.2",
"electron": "^2.0.0",
"fs-extra": "^7.0.0",
Expand All @@ -59,11 +60,14 @@
"rollup-plugin-license": "^0.6.0",
"rollup-plugin-node-resolve": "^3.3.0",
"rollup-plugin-replace": "^2.0",
"rollup-plugin-typescript2": "^0.14.0",
"rollup-plugin-visualizer": "^0.6.0",
"sinon": "^5.0",
"standard": "^11.0.1",
"typescript": "^2.8.3"
"rollup-plugin-typescript2": "^0.20.1",
"rollup-plugin-visualizer": "^0.3.1",
"sinon": "^4.1",
"standard": "^10.0.3",
"tslib": "^1.9.3",
"tslint": "5.9.1",
"tslint-config-standard": "^8.0.1",
"typescript": "^3.3.3333"
},
"workspaces": [
"packages/*"
Expand Down
2 changes: 1 addition & 1 deletion packages/computed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dependencies": {
"@tko/observable": "^4.0.0-alpha7.4",
"@tko/utils": "^4.0.0-alpha7.4",
"tslib": "^1.8.0"
"tslib": "^1.9.3"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/filter.punches/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"homepage": "https://tko.io",
"dependencies": {
"@tko/observable": "^4.0.0-alpha7.4",
"tslib": "^1.8.0"
"tslib": "^1.9.3"
},
"karma": {
"frameworks": [
Expand Down
1 change: 0 additions & 1 deletion packages/node_modules

This file was deleted.

2 changes: 1 addition & 1 deletion packages/observable/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"homepage": "https://tko.io",
"dependencies": {
"@tko/utils": "^4.0.0-alpha7.4",
"tslib": "^1.8.0"
"tslib": "^1.9.3"
},
"karma": {
"frameworks": [
Expand Down
32 changes: 0 additions & 32 deletions packages/observable/src/Subscription.js

This file was deleted.

42 changes: 42 additions & 0 deletions packages/observable/src/Subscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

import {
removeDisposeCallback, DisposeCallback, addDisposeCallback
} from '@tko/utils'
import { ISubscribable } from './subscribable'

export const LATEST_VALUE = Symbol('Knockout latest value')

export default class Subscription<T> {
private _domNodeDisposalCallback: null|DisposeCallback
private _node: null|Node
private _isDisposed: boolean
private _callback: null|((value: T) => void)

constructor (
private _target: ISubscribable<T>,
observer,
private _disposeCallback: DisposeCallback
) {
this._callback = observer.next
this._domNodeDisposalCallback = null
this._node = null
this._isDisposed = false
}

dispose (): void {
if (this._domNodeDisposalCallback) {
removeDisposeCallback(this._node, this._domNodeDisposalCallback)
}
this._isDisposed = true
this._disposeCallback()
}

disposeWhenNodeIsRemoved (node: Node): void {
this._node = node
addDisposeCallback(node, this._domNodeDisposalCallback = this.dispose.bind(this))
}

// TC39 Observable API
unsubscribe () : void { this.dispose() }
get closed () : boolean { return this._isDisposed }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@ import {

import * as dependencyDetection from './dependencyDetection.js'
import { deferUpdates } from './defer.js'
import { subscribable, defaultEvent, LATEST_VALUE } from './subscribable.js'
import { subscribable, defaultEvent } from './subscribable'
import { LATEST_VALUE } from './Subscription'
import { valuesArePrimitiveAndEqual } from './extenders.js'

export function observable (initialValue) {
function Observable () {
if (arguments.length > 0) {
interface IObservable<T> {
(this: IObservable<T>, ...args: any[]): IObservable<T>
isDifferent: (latest: T, other: T) => boolean
[LATEST_VALUE]: T
valueWillMutate: () => void
valueHasMutated: () => void
isWriteable: boolean
}

export function observable<T> (initialValue: T) : Observable<T> {
const Observable: IObservable<T> = function (this: IObservable<T>, ...args: any[]) : T|IObservable<T> {
if (args.length > 0) {
// Write
// Ignore writes if the value hasn't changed
if (Observable.isDifferent(Observable[LATEST_VALUE], arguments[0])) {
Expand Down Expand Up @@ -48,7 +58,7 @@ export function observable (initialValue) {
// Define prototype for observables
observable.fn = {
equalityComparer: valuesArePrimitiveAndEqual,
peek () { return this[LATEST_VALUE] },
peek (this: IObservable<T>) : T { return this[LATEST_VALUE] },
valueHasMutated () {
this.notifySubscribers(this[LATEST_VALUE], 'spectate')
this.notifySubscribers(this[LATEST_VALUE])
Expand Down Expand Up @@ -144,23 +154,23 @@ observable.fn[protoProperty] = observable
// isObservable will be `true`.
observable.observablePrototypes = new Set([observable])

export function isObservable (instance) {
export function isObservable<T> (instance: T|IObservable<T>) {
const proto = typeof instance === 'function' && instance[protoProperty]
if (proto && !observable.observablePrototypes.has(proto)) {
throw Error('Invalid object that looks like an observable; possibly from another Knockout instance')
}
return !!proto
}

export function unwrap (value) {
export function unwrap<T> (value: T|IObservable<T>) {
return isObservable(value) ? value() : value
}

export function peek (value) {
export function peek<T> (value: T|IObservable<T>) {
return isObservable(value) ? value.peek() : value
}

export function isWriteableObservable (instance) {
export function isWriteableObservable<T> (instance: T|IObservable<T>) {
return isObservable(instance) && instance.isWriteable
}

Expand Down
2 changes: 1 addition & 1 deletion packages/observable/src/observableArray.changeTracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
extend, compareArrays, findMovesInArrayComparison
} from '@tko/utils'

import { defaultEvent } from './subscribable.js'
import { defaultEvent } from './subscribable'
import { extenders } from './extenders.js'

export var arrayChangeEventName = 'arrayChange'
Expand Down
Loading