-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(atom): re-import atom package from MBP2010, update main readme
- Loading branch information
1 parent
bddd5ce
commit fefc283
Showing
9 changed files
with
222 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/bench | ||
/build | ||
/dev | ||
/doc | ||
/node_modules | ||
.DS_Store | ||
/bundle.* | ||
*.log | ||
*.tgz | ||
*.js | ||
*.d.ts |
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,14 @@ | ||
bench/* | ||
build/* | ||
dev/* | ||
node_modules | ||
src* | ||
test* | ||
bundle.* | ||
tsconfig.json | ||
webpack.config.js | ||
*.html | ||
*.tgz | ||
!doc/* | ||
!*.d.ts | ||
!*.js |
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,43 @@ | ||
# @thi.ng/atom | ||
|
||
[![npm (scoped)](https://img.shields.io/npm/v/@thi.ng/atom.svg)](https://www.npmjs.com/package/@thi.ng/atom) | ||
|
||
## About | ||
|
||
Clojure inspired mutable wrapper for (usually) immutable values, with support for watches. | ||
|
||
## Installation | ||
|
||
``` | ||
yarn add @thi.ng/atom | ||
``` | ||
|
||
## Usage examples | ||
|
||
```typescript | ||
import { Atom } from "@thi.ng/atom"; | ||
|
||
const a = new Atom(23); | ||
|
||
// obtain value via deref() | ||
a.deref(); | ||
// 23 | ||
|
||
// add watch to observe value changes | ||
a.addWatch("foo", (id, prev, curr) => console.log(`${id}: ${prev} -> ${curr}`)); | ||
// true | ||
|
||
a.swap((val)=> val + 1); | ||
// foo: 23 -> 24 | ||
|
||
a.reset(42); | ||
// foo: 24 -> 42 | ||
``` | ||
|
||
## Authors | ||
|
||
- Karsten Schmidt | ||
|
||
## License | ||
|
||
© 2018 Karsten Schmidt // Apache Software License 2.0 |
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,35 @@ | ||
{ | ||
"name": "@thi.ng/atom", | ||
"version": "0.0.1", | ||
"description": "Mutable wrapper for a immutable values", | ||
"main": "./index.js", | ||
"typings": "./index.d.ts", | ||
"repository": "https://github.com/thi-ng/umbrella", | ||
"author": "Karsten Schmidt <k+npm@thi.ng>", | ||
"license": "Apache-2.0", | ||
"scripts": { | ||
"build": "yarn run clean && tsc --declaration", | ||
"test": "yarn run clean && tsc -p test && mocha build/test/*.js", | ||
"clean": "rm -rf *.js *.d.ts build doc", | ||
"pub": "yarn run build && yarn publish --access public" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "^2.2.46", | ||
"@types/node": "^9.3.0", | ||
"mocha": "^5.0.0", | ||
"ts-loader": "^3.3.1", | ||
"typedoc": "^0.9.0", | ||
"typescript": "^2.6.2", | ||
"webpack": "^3.10.0" | ||
}, | ||
"dependencies": { | ||
"@thi.ng/api": "^1.3.0" | ||
}, | ||
"keywords": [ | ||
"ES6", | ||
"typescript" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
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,55 @@ | ||
import * as api from "@thi.ng/api/api"; | ||
import { IWatch } from "@thi.ng/api/mixins/iwatch"; | ||
|
||
/** | ||
* Mutable wrapper for an (usually) immutable value. | ||
* Support for watches. | ||
*/ | ||
@IWatch | ||
export class Atom<T> implements | ||
api.IDeref<T>, | ||
api.IEquiv, | ||
api.IWatch<T> { | ||
|
||
protected value: T; | ||
|
||
constructor(val?: T) { | ||
this.value = val; | ||
} | ||
|
||
public deref() { | ||
return this.value; | ||
} | ||
|
||
public equiv(o: any) { | ||
return this === o; | ||
} | ||
|
||
public reset(val: T) { | ||
const old = this.value; | ||
this.value = val; | ||
this.notifyWatches(old, val); | ||
return this.value; | ||
} | ||
|
||
public swap(fn: (curr: T, ...args: any[]) => T, ...args: any[]) { | ||
const old = this.value; | ||
args.unshift(old); | ||
this.value = fn.apply(null, args); | ||
this.notifyWatches(old, this.value); | ||
return this.value; | ||
} | ||
|
||
// mixin stub | ||
public addWatch(id: string, fn: (id: string, oldState: T, newState: T) => void) { | ||
return false; | ||
} | ||
|
||
// mixin stub | ||
public removeWatch(id: string) { | ||
return false; | ||
} | ||
|
||
// mixin stub | ||
public notifyWatches(oldState: T, newState: T) { } | ||
} |
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,42 @@ | ||
import * as assert from "assert"; | ||
import { Atom } from "../src/index"; | ||
|
||
describe("atom", function () { | ||
|
||
let a: Atom<any>; | ||
|
||
beforeEach(() => { | ||
a = new Atom(23); | ||
}); | ||
|
||
it("can be deref'd", () => { | ||
assert.equal(a.deref(), 23); | ||
}); | ||
|
||
it("can be reset", () => { | ||
assert.equal(a.reset(24), 24); | ||
assert.equal(a.deref(), 24); | ||
}); | ||
|
||
it("can be swapped", () => { | ||
assert.equal(a.swap((x) => x + 1), 24); | ||
assert.equal(a.deref(), 24); | ||
}); | ||
|
||
it("can add & remove watch", () => { | ||
assert(a.addWatch("foo", () => { }), "can't add watch"); | ||
assert((<any>a)._watches && (<any>a)._watches.foo, "watch missing"); | ||
assert(a.removeWatch("foo"), "can't remove watch"); | ||
assert(!a.removeWatch("foo"), "should fail to remove invalid watch id"); | ||
}); | ||
|
||
it("can be watched", () => { | ||
a.addWatch("foo", (id, prev, curr) => { | ||
assert.equal(id, "foo", "wrong id"); | ||
assert.equal(prev, 23, "wrong prev"); | ||
assert.equal(curr, 24, "wrong curr"); | ||
}); | ||
a.swap((x) => x + 1); | ||
}); | ||
|
||
}); |
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,11 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../build", | ||
"noUnusedParameters": false | ||
}, | ||
"include": [ | ||
"./**/*.ts", | ||
"../src/**/*.ts" | ||
] | ||
} |
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,10 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": ".", | ||
"noUnusedParameters": false, | ||
}, | ||
"include": [ | ||
"./src/**/*.ts", | ||
] | ||
} |