Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 # method
3 [](http://travis-ci.org/Gozala/method)
5 Library provides an API for defining polymorphic methods that dispatch on the
6 first argument type. This provides a powerful way for decouple abstraction
7 interface definition from an actual implementation per type, without risks
8 of interference with other libraries.
10 ### Motivation
12 - Provide a high-performance, dynamic polymorphism construct as an
13 alternative to existing object methods that does not provides any
14 mechanics for guarding against name conflicts.
15 - Allow independent extension of types, and implementations of methods
16 on types, by different parties.
18 ## Install
20 npm install method
22 ## Use
24 ```js
25 var method = require("method")
27 // Define `isWatchable` method that can be implemented for any type.
28 var isWatchable = method("isWatchable")
30 // If you call it on any object it will
31 // throw as nothing implements that method yet.
32 //isWatchable({}) // => Exception: method is not implemented
34 // If you define private method on `Object.prototype`
35 // all objects will inherit it.
36 Object.prototype[isWatchable] = function() {
37 return false;
38 }
40 isWatchable({}) // => false
43 // Although `isWatchable` property above will be enumerable and there for
44 // may damage some assumbtions made by other libraries. There for it"s
45 // recomended to use built-in helpers methods that will define extension
46 // without breaking assumbtions made by other libraries:
48 isWatchable.define(Object, function() { return false })
51 // There are primitive types in JS that won"t inherit methods from Object:
52 isWatchable(null) // => Exception: method is not implemented
54 // One could either implement methods for such types:
55 isWatchable.define(null, function() { return false })
56 isWatchable.define(undefined, function() { return false })
58 // Or simply define default implementation:
59 isWatchable.define(function() { return false })
61 // Alternatively default implementation may be provided at creation:
62 isWatchable = method(function() { return false })
64 // Method dispatches on an first argument type. That allows us to create
65 // new types with an alternative implementations:
66 function Watchable() {}
67 isWatchable.define(Watchable, function() { return true })
69 // This will make all `Watchable` instances watchable!
70 isWatchable(new Watchable()) // => true
72 // Arbitrary objects can also be extended to implement given method. For example
73 // any object can simply made watchable:
74 function watchable(object) {
75 return isWatchable.implement(objct, function() { return true })
76 }
78 isWatchable(watchable({})) // => true
80 // Full protocols can be defined with such methods:
81 var observers = "observers@" + module.filename
82 var watchers = method("watchers")
83 var watch = method("watch")
84 var unwatch = method("unwatch")
86 watchers.define(Watchable, function(target) {
87 return target[observers] || (target[observers] = [])
88 })
90 watch.define(Watchable, function(target, watcher) {
91 var observers = watchers(target)
92 if (observers.indexOf(watcher) < 0) observers.push(watcher)
93 return target
94 })
95 unwatch.define(Watchable, function(target, watcher) {
96 var observers = watchers(target)
97 var index = observers.indexOf(watcher)
98 if (observers.indexOf(watcher) >= 0) observers.unshift(watcher)
99 return target
100 })
102 // Define type Port that inherits form Watchable
104 function Port() {}
105 Port.prototype = Object.create(Watchable.prototype)
107 var emit = method("emit")
108 emit.define(Port, function(port, message) {
109 watchers(port).slice().forEach(function(watcher) {
110 watcher(message)
111 })
112 })
114 var p = new Port()
115 watch(p, console.log)
116 emit(p, "hello world") // => info: "hello world"
117 ```